package sisi.yacht;

import java.util.List;

import javax.persistence.EntityManager;


/**
 * AnayachtController.java - Controller class for the <i>anayacht database table</i>. 
 * @author Pedro Gonzalez pedro@sisisoftware.it
 * @author Claudio Pelosi claudio@sisisoftware.it
 * @author SISI Software S.n.c. - www.sisisoftware.it
 * @version 1.0, 09/23/2013
 * 
 */

public class AnayachtController {

	private EntityManager getEntityManager() {
		return new sisi.MyEmf().getEntityManager();
	}

	public Anayacht[] getAnayacht(String codice) {
		List<Anayacht> yachts = getAnayachtList(codice);
		return yachts.toArray(new Anayacht[0]);
	}

	@SuppressWarnings("unchecked")
	public List<Anayacht> getAnayachtList(String cerca) {

		String cWhere = "";
		if (cerca != null && !cerca.isEmpty()) {
			cWhere = " WHERE TRIM( BOTH FROM UPPER(c.name) ) LIKE :cerca ";
		}

		EntityManager em = getEntityManager();
		try {
			javax.persistence.Query q = em
					.createQuery("select c from Anayacht as c " + cWhere
							+ " order by c.name");
			
			if (cerca != null && !cerca.isEmpty()) {
				q.setParameter("cerca", '%'+cerca.trim().toUpperCase()+'%' );
			}
			
			List<Anayacht> ar1 = q.getResultList();
			return ar1;
		} finally {
			em.close();
		}
	}

	public Anayacht getAnayachtxId(int id) {

		EntityManager em = getEntityManager();
		try {
			Anayacht anayachtx = em.find(Anayacht.class, id);
			return anayachtx;
		} finally {
			em.close();
		}
	}

	@SuppressWarnings("finally")
	public Anayacht addAnayacht(Anayacht yacht) {
		EntityManager em = getEntityManager();
		try {
			em.getTransaction().begin();
			em.persist(yacht);
			em.getTransaction().commit();
			yacht = refreshAnayacht(yacht);
		} finally {
			em.close();
			return yacht;
		}
	}

	@SuppressWarnings("finally")
	public boolean updateAnayacht(Anayacht yacht) {

		EntityManager em = getEntityManager();
		try {
			em.getTransaction().begin();
			Anayacht yachtx = em.find(Anayacht.class, yacht.getId());
			yachtx.setId(yacht.getId());
			yachtx.setBank(yacht.getBank());
			yachtx.setBankpay(yacht.getBankpay());
			yachtx.setBeam(yacht.getBeam());
			yachtx.setBoatgsm(yacht.getBoatgsm());
			yachtx.setBroker(yacht.getBroker());
			yachtx.setBrokerid(yacht.getBrokerid());
			yachtx.setCallsign(yacht.getCallsign());
			yachtx.setCaptain(yacht.getCaptain());
			yachtx.setCaptainid(yacht.getCaptainid());
			yacht.setCaptgsm(yacht.getCaptgsm());
			yachtx.setCetificate(yacht.getCetificate());
			yachtx.setChiefeng(yacht.getChiefeng());
			yachtx.setChiefgsm(yacht.getChiefgsm());
			yachtx.setCode(yacht.getCode());
			yachtx.setContact1(yacht.getContact1());
			yachtx.setContact2(yacht.getContact2());
			yachtx.setCorpid(yacht.getCorpid());
			yachtx.setCorporation(yacht.getCorporation());
			yachtx.setCum(yacht.getCum());
			yachtx.setDel(yacht.getDel());
			yachtx.setDraft(yacht.getDraft());
			yachtx.setDtup(yacht.getDtup());
			yachtx.setDum(yacht.getDum());
			yachtx.setEmail(yacht.getEmail());
			yachtx.setEngnumb(yacht.getEngnumb());
			yachtx.setEngtype(yacht.getEngtype());
			yachtx.setFiscstat(yacht.getFiscstat());
			yachtx.setFlag(yacht.getFlag());
			yachtx.setFuelconn(yacht.getFuelconn());
			yachtx.setFueltnkcapy(yacht.getFueltnkcapy());
			yachtx.setGrton(yacht.getGrton());
			yachtx.setGsm(yacht.getGsm());
			yachtx.setImonumber(yacht.getImonumber());
			yachtx.setLoa(yacht.getLoa());
			yachtx.setLum(yacht.getLum());
			yachtx.setName(yacht.getName());
			yachtx.setNetton(yacht.getNetton());
			yachtx.setNote(yacht.getNote());
			yachtx.setOilused(yacht.getOilused());
			yachtx.setPayment(yacht.getPayment());
			yachtx.setPhoto(yacht.getPhoto());
			yachtx.setPortreg(yacht.getPortreg());
			yachtx.setPumprate(yacht.getPumprate());
			yachtx.setSatcomfax(yacht.getSatcomfax());
			yachtx.setSatcomtel(yacht.getSatcomtel());
			yachtx.setShipyard(yacht.getShipyard());
			yachtx.setVesstype(yacht.getVesstype());
			yachtx.setWum(yacht.getWum());

			em.getTransaction().commit();
			em.flush();

		} finally {
			em.close();
			return false;
		}
	}

	@SuppressWarnings("finally")
	public Anayacht refreshAnayacht(Anayacht yacht) {

		Anayacht yachtx = yacht;
		EntityManager em = getEntityManager();
		try {
			em.getTransaction().begin();
			yachtx = em.find(Anayacht.class, yacht.getId());
			em.flush();
			em.refresh(yachtx);
		} finally {
			em.close();
			return yachtx;
		}
	}

	@SuppressWarnings("finally")
	public boolean removeAnayacht(Anayacht yacht) {
		EntityManager em = getEntityManager();
		try {
			em.getTransaction().begin();
			Anayacht yachtx = em.find(Anayacht.class, yacht.getId());
			em.remove(yachtx);
			em.getTransaction().commit();
		} finally {
			em.close();
			return false;
		}
	}

}
